package com.nimt.hqgl_idea.service;

import com.nimt.hqgl_idea.entity.User;
import com.nimt.hqgl_idea.mapper.UserMapper;
import com.nimt.hqgl_idea.param.UserParam;
import com.nimt.hqgl_idea.util.ErrorEnum;
import com.nimt.hqgl_idea.util.ResponseBody;
import com.nimt.hqgl_idea.vo.PwdVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    /*自动装配*/
    @Autowired
    UserMapper userMapper;

    @Override
    public ResponseBody login(User user) {
        ResponseBody responseBody = null;
        User dbUser = userMapper.selectById(user.getId());
        if (dbUser != null){
            if (dbUser.getPassword().equals(user.getPassword())){
                if (dbUser.getRole().equals(user.getRole())){
                    responseBody = new ResponseBody(dbUser);
                }else {
                    responseBody = new ResponseBody(ErrorEnum.Role_Invalid);
                }
            }else {
                responseBody  =new ResponseBody(ErrorEnum.Password_Invalid);
            }
        }else {
            responseBody = new ResponseBody(ErrorEnum.Account_Not_Exist);
        }
        return responseBody;
    }

    @Override
    public ResponseBody add(User user) {
        ResponseBody responseBody = null;
        userMapper.insert(user);
        if(user.getId()!=null){
            responseBody = new ResponseBody(user);
        }else {
            responseBody = new ResponseBody(ErrorEnum.Create_Account_Error);
        }
        return responseBody;
    }

    @Override
    public ResponseBody updatePwd(PwdVo pwdVo) {
        ResponseBody responseBody = null;
        User user = userMapper.selectById(pwdVo.getId());
        if(!user.getPassword().equals(pwdVo.getOldPwd())){
            responseBody= new ResponseBody(ErrorEnum.Old_Password_Error);
        } else if(user.getPassword().equals(pwdVo.getNewPwd())) {
            responseBody = new ResponseBody(ErrorEnum.Password_Repeat_Error);
        }else {
            User pwdUser = new User();
            pwdUser.setId(pwdVo.getId());
            pwdUser.setPassword(pwdVo.getNewPwd());
            userMapper.update(pwdUser);
            responseBody = new ResponseBody("修改成功!");
        }
        return responseBody;
    }
    @Override
    public ResponseBody selectOne(User user) {
        ResponseBody responseBody = null;
        User userOne= userMapper.selectById(user.getId());
        if (userOne != null){
            responseBody = new ResponseBody(userOne);
        } else {
            responseBody = new ResponseBody(ErrorEnum.Account_Not_Exist);
        }
        return  responseBody;
    }

    @Override
    public ResponseBody updateInfo(User user){
        ResponseBody responseBody = null;
        User userOne = userMapper.selectById(user.getId());
        if (userOne != null) {
            userMapper.update(user);
            responseBody = new ResponseBody(user);
        } else {
            responseBody = new ResponseBody(ErrorEnum.Account_Modify_error);
        }
        return responseBody;
    }

    @Override
    public ResponseBody query(UserParam userParam) {
        ResponseBody responseBody = null;
        userParam.setTotalCount(userMapper.count(userParam.getKeywords(),userParam.getRole()));
        int start = (userParam.getCurrentPage()-1)*userParam.getPageSize();
        List<User> users = userMapper.getAllUsers(start,userParam.getPageSize(),userParam.getKeywords(),userParam.getRole());
        if (users == null || users.size() == 0){
            responseBody = new ResponseBody(ErrorEnum.No_More_Data);
        }else {
            userParam.setData(users);
            responseBody  = new ResponseBody(userParam);
        }
       return responseBody;
    }

    @Override
    public ResponseBody delete(Integer id) {
        ResponseBody responseBody = null;
        if(userMapper.delete(id)>0){
            responseBody = new ResponseBody("删除成功!");
        }else {
            responseBody = new ResponseBody(ErrorEnum.Delete_Error);
        }
        return responseBody;
    }
}
